package com.sjp.demo.netty.rpc.registry;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;

public class RpcRegistry {
    private int port;

    public RpcRegistry(int port) {
        this.port = port;
    }

    public void start() {
        try {
            // netty基于NIO
            // Selector 主线程池
            EventLoopGroup bossGroup = new NioEventLoopGroup();
            // 子线程池 Work线程
            EventLoopGroup workGroup = new NioEventLoopGroup();

            ServerBootstrap server = new ServerBootstrap();
            server.group(bossGroup, workGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            //在netty中，把所有的业务逻辑处理全部归总到一个队列中
                            //这个队列中，包含了各种各样的处理逻辑，对这些处理逻辑，在netty中有一个封装
                            //封装成一个对象，无锁化串行任务队列
                            // Pipline
                            ChannelPipeline pipeline = ch.pipeline();

                            // 就是对我们处理逻辑的封装
                            //对于自定义协议的内容要进行编，解码
                            pipeline.addLast(
                                    new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
                            pipeline.addLast(new LengthFieldPrepender(4));

                            //实参处理
                            pipeline.addLast("encoder", new ObjectEncoder());
                            pipeline.addLast("decoder", new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
                            // 数据解析结束

                            // 执行属于自己的逻辑
                            // 1、注册，给每个对象起一个名字，对外提供服务的名字
                            // 2、服务位置要做一个登记
                            pipeline.addLast(new RegistryHandler());

                        }
                    })
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);


            // 正式启动服务，相当于用一个死循环开始轮询
            ChannelFuture future = server.bind(this.port).sync();
            System.out.println("RPC Registry start at port: " + this.port);
            future.channel().closeFuture().sync();


        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public static void main(String[] args) {
        new RpcRegistry(8080).start();
    }
}
